package com.wnhz.work.yz.activity.register;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.CalUtil;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * ---注册
 */
public class RegisterActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_right;
    private EditText et_phone;

    private String phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initData();
        initView();
    }

    private void initData() {

    }

    private void initView() {
        findViewById(R.id.view_main_middle_title).setVisibility(View.GONE);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.btn_main_right).setVisibility(View.VISIBLE);
        findViewById(R.id.btn_main_right).setOnClickListener(this);
        tv_right = (TextView) findViewById(R.id.view_main_right_text);
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("下一步");
        tv_right.setTextColor(0xff008c99);

        et_phone = (EditText) findViewById(R.id.et_phone);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //下一步
            case R.id.btn_main_right:
                phone = et_phone.getText().toString();
                if (!"".equals(phone)) {
                    if (phone.length() == 11) {

                        upload();
                    } else {
                        MyToast("请输入正确的手机号码!");
                    }
                } else {
                    MyToast("手机号码不能为空");
                }
                break;

            default:
                break;
        }
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("type", 3);
        map.put("other", CalUtil.Cal());
        XUtil.Post(Url.PHONENOEXISTSENDCODE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);
                Log.i("= 注册获取验证码 =", "onSuccess: phone= " + result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String message = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(message);
                        Intent intent = new Intent(RegisterActivity.this, Register2Activity.class);
                        intent.putExtra("phone", phone);
                        startActivity(intent);
                    } else {
                        MyToast(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

}
